package com.shuqi.application;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.shuqi.database.model.BookCataLog;

public class CataLogCache {
    private static CataLogCache instance = null;

    private List<BookCataLog> mLocalCatalogList = null;

    private String mKey;

    private CataLogCache() {

    }

    public static synchronized CataLogCache getInstance() {
        if (instance == null) {
            instance = new CataLogCache();
        }
        return instance;
    }

    public void initCache(String bookId, String sourceId, String uid,
            List<BookCataLog> localCatalogList) {
        String key = createKey(bookId, sourceId, uid);
        if (key != null && localCatalogList != null) {
            mKey = key;
            mLocalCatalogList = localCatalogList;
        }
    }

    public void destroy() {
        mKey = null;
        mLocalCatalogList = null;
    }

    public BookCataLog getBookCatalogByCid(String uid, String bookId, String sourceId,
            String chapterid) {
        if (mLocalCatalogList != null) {
            String key = createKey(bookId, sourceId, uid);
            if (key != null && key.equals(mKey)) {
                for (int i = 0; i < mLocalCatalogList.size(); i++) {
                    BookCataLog cataLog = mLocalCatalogList.get(i);
                    if (chapterid.equals(cataLog.getChapterId())) {
                        return cataLog;
                    }
                }
            }
        }
        return null;
    }

    public HashSet<String> getNeedDownLoadCidSet(String uid, String sourceId, String bookId,
            HashSet<String> cidSet) {
        if (mLocalCatalogList == null) {
            return null;
        } else {
            HashSet<String> chapterSet = new HashSet<String>();
            String key = createKey(bookId, sourceId, uid);
            if (key != null && key.equals(mKey)) {
                for (int i = 0; i < mLocalCatalogList.size(); i++) {
                    BookCataLog cataLog = mLocalCatalogList.get(i);
                    if (cidSet.contains(cataLog.getChapterId())
                            && cataLog.getDownloadState() == BookCataLog.UN_DOWN) {
                        chapterSet.add(cataLog.getChapterId());
                    }
                }
            } else {
                return null;
            }
            return chapterSet;
        }
    }

    public List<BookCataLog> getBookCatalogListFromChapterIndex(String uid, String bookId,
            String sourceId, int index, int pageSize) {
        if (mLocalCatalogList == null) {
            return null;
        } else {
            String key = createKey(bookId, sourceId, uid);
            if (key != null && key.equals(mKey)) {
                List<BookCataLog> list = new ArrayList<BookCataLog>();
                for (int i = 0; i < mLocalCatalogList.size(); i++) {
                    BookCataLog cataLog = mLocalCatalogList.get(i);
                    if (cataLog.getOId() >= index) {
                        list.add(cataLog);
                        pageSize--;
                    }
                    if (pageSize == 0) {
                        break;
                    }
                }
                return list;
            } else {
                return null;
            }
        }
    }


    private static String createKey(String bookId, String sourceId, String uid) {
        if (bookId != null) {
            if (sourceId == null) {
                sourceId = "";
            }
            return bookId + "_" + sourceId + "_" + uid;
        }
        return null;
    }
}
